<template>
  <div class="pagination">
    <button @click="changePage(currentPage -1)" :disabled="currentPage === 1">上一页</button>
    <button v-if="startEnd.start > 1" @click="changePage(1)">1</button>
    <button v-if="startEnd.start > 2">···</button>
    <!-- 连续页 -->
    <button v-for="num in pageArr" :key="num" :class="{active:currentPage === num}"
      @click="changePage(num)">{{ num }}
    </button>

    
    <button v-if="startEnd.end < totalPage-1">···</button>
    <button v-if="startEnd.end < totalPage" @click="changePage(totalPage)">{{totalPage}}</button>
    <button :disabled="currentPage === totalPage" @click="changePage(currentPage  + 1)">下一页</button>
    
    <button style="margin-left: 30px">共 {{total}} 条</button>
  </div>
</template>

<script>
  export default {
    name: "Pagination",
    props:{
      currentPage:{
        type:Number,
        default:0
      },
      total:{
        type:Number,
        default:0
      },
      pageSize:{
        type:Number,
        default:0
      },
      continueNum:{
        type:Number,
        default:0
      }
    },
    computed:{
      totalPage(){
        return Math.ceil(this.total/this.pageSize);
      },
      //连续页的起始和结束
      startEnd(){       
        let start = 0;
        let end = 0;
        let {currentPage,continueNum,totalPage} = this;
        //判断:排除特殊情况
        if (continueNum > totalPage) {
          start = 1;
          end = totalPage;
        } else {
          //计算起始和结束
          start = currentPage - Math.floor(continueNum/2 );
          end = currentPage + Math.floor(continueNum/2);
          //修正
          if (start < 1) {
            start = 1;
            end = continueNum;
          }
          if (end > totalPage) {
            end = totalPage;
            // 比如：总页数10，连续页5，就是 6,7,8,9,10
            start = totalPage - continueNum + 1;
          }
        }
        return {start,end};
      },
      //计算连续页数组
      pageArr(){
        let arr = [];
        for(let i = this.startEnd.start; i <= this.startEnd.end; i ++) {
          arr.push(i);
        }
        return arr;
      }
    },
    methods:{
      //prevPage(){
        //this.$emit('searchForPageNo',this.currentPage - 1);
     // },
      //nextPage(){
        //this.$emit('searchForPageNo',this.currentPage + 1);
     // },
      changePage(num){
        // this.$emit('searchForPageNo',num);
        // 要通用，最好自定义事件名也通用
        this.$emit('changePage',num);
      }
    }
  }
</script>

<style lang="sass" scoped>
  .pagination {
    button {
      margin: 0 5px;
      background-color: #f4f4f5;
      color: #606266;
      outline: none;
      border-radius: 2px;
      padding: 0 4px;
      vertical-align: top;
      display: inline-block;
      font-size: 13px;
      min-width: 35.5px;
      height: 28px;
      line-height: 28px;
      cursor: pointer;
      box-sizing: border-box;
      text-align: center;
      border: 0;

      &[disabled] {
        color: #c0c4cc;
        cursor: not-allowed;
      }

      &.active {
        cursor: not-allowed;
        background-color: #409eff;
        color: #fff;
      }
    }
  }
</style>
